package com.xie.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xie.pojo.Shouke;
import com.xie.service.ShoukeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/shouke")
public class ShoukeController {

    @Autowired
    @Qualifier("ShoukeServiceImpl")
    private ShoukeService shoukeService;

    @RequestMapping("/allShouke")
    public String allShouke(HttpSession session){
        List<Shouke> shoukes = shoukeService.queryAllShouke();
        session.setAttribute("shoukeList",shoukes);
        return "shouke";
    }

    @RequestMapping("/deleteShouke")
    public String deleteShouke(String classname, Model model){
        int i = shoukeService.deleteShouke(classname);
        if(i > 0){
            return "redirect:/shouke/allShouke";
        }else{
            model.addAttribute("msg","删除失败");
            return "redirect:/shouke/allShouke";
        }
    }

    @RequestMapping("/toAddShouke")
    public String toAddShouke(){
        return "addShouke";
    }
    @RequestMapping("/addShouke")
    public String addShouke(Shouke shouke, Model model){
        int i = shoukeService.addShouke(shouke);
        if (i > 0){
            return "redirect:/shouke/allShouke";
        }else{
            model.addAttribute("msg","添加失败");
            return "redirect:/shouke/toAddShouke";
        }
    }

    @RequestMapping("/toUpdateShouke")
    public String toUpdateShouke(String classname,Model model){
        Shouke shouke = shoukeService.queryShouke(classname);
        model.addAttribute("QShouke",shouke);
        return "updateShouke";
    }
    @RequestMapping("/updateShouke")
    public String updateShouke(Shouke shouke,Model model){
        int i = shoukeService.updateShouke(shouke);
        if (i > 0){
            return "redirect:/shouke/allShouke";
        }else{
            model.addAttribute("msg","修改失败");
            return "redirect:/shouke/allShouke";
        }
    }












}
